// Fill out your copyright notice in the Description page of Project Settings.


#include "Casing.h"
#include "Components/StaticMeshComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundBase.h"


ACasing::ACasing()
{
	PrimaryActorTick.bCanEverTick = false;
	CasingMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CasingMesh"));
	SetRootComponent(CasingMesh);
	CasingMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
	//SetSimulatePhysics：设置仿真模拟物理
	CasingMesh->SetSimulatePhysics(true);
	//SetEnableGravity：设置是否能够重力
	CasingMesh->SetEnableGravity(true);
	//SetNotifyRigidBodyCollision：设置是否应该收到物理碰撞通知
	CasingMesh->SetNotifyRigidBodyCollision(true);
	ShellEjectionImpulse = 10.f;
}

void ACasing::BeginPlay()
{
	Super::BeginPlay();
	//AddImpulse：添加脉冲去给一个刚体，适合瞬时脉冲
	CasingMesh->AddImpulse(GetActorForwardVector() * ShellEjectionImpulse);
	CasingMesh->OnComponentHit.AddDynamic(this, &ACasing::OnHit);
}

void ACasing::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
                    FVector NormalImpulse, const FHitResult& Hit)
{
	if(ShellSound)
	{
		UGameplayStatics::PlaySoundAtLocation(this, ShellSound, GetActorLocation());
	}
	//SetLifeSpan：设置Actor寿命，到时间后将会销毁
	// SetLifeSpan(3.f);
	Destroy();
}
